/* 
 * Copyright 2014 ShangDao.Ltd  All rights reserved.
 * SiChuan ShangDao.Ltd PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * @ActPostList.java  2014-9-16 下午5:01:13 - Carson
 * @author Guzhu
 * @email:muyi126@163.com
 * @version 1.0
 */

package com.android.tiango.baba.act;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.android.scsd.tiango.R;
import com.android.support.httpclient.HttpCallBack;
import com.android.support.httpclient.HttpClientAsync;
import com.android.support.httpclient.HttpParams;
import com.android.tiango.baba.BaListActivity;
import com.android.tiango.baba.cookie.HttpUrl;
import com.android.tiango.baba.entity.SimpleTopicEntity;
import com.android.tiango.baba.entity.SimpleTopicModel;
import com.android.tiango.baba.util.Constants;
import com.android.tiango.baba.util.DensityUtil;
import com.android.tiango.baba.util.StringUtils;
import com.android.tiango.baba.util.TimeToUtil;
import com.android.tiango.baba.util.ToastUtils;
import com.android.tiango.baba.view.AppTitleBar;
import com.android.tiango.baba.view.PullListView.PullListViewListener;

/**
 * 精华帖
 * @author YW_pc
 * 
 */
public class ActGoodsPostList extends BaListActivity<SimpleTopicModel> implements PullListViewListener {
	private String barId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		if (intent.hasExtra(Constants.BARID)) {
			barId = intent.getStringExtra(Constants.BARID);
		} else {
			finish();
		}
		mTitleBar = (AppTitleBar) findViewById(R.id.titlebar);
		if (null != mTitleBar) {
			mTitleBar.setTitle("精华帖");
			mTitleBar.setImageBackGround(R.drawable.bg_title_blue);
			mTitleBar.setBackIcon(R.drawable.icon_back);
			mListView.setDividerHeight(DensityUtil.px2dip(this, 15));

			LayoutParams param = (LayoutParams) mListView.getLayoutParams();
			param.setMargins(DensityUtil.px2dip(this, 30), 0, DensityUtil.px2dip(this, 30), 0);
			mListView.setLayoutParams(param);
			mListView.setPullListener(this);
			mListView.startOnRefresh();
			setBackOnClickListener();
		}
	}

	@Override
	protected View getItemView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (null == convertView) {
			convertView = LayoutInflater.from(this).inflate(R.layout.item_post, null);
			holder = new ViewHolder();
			holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
			holder.iv_actre = (ImageView) convertView.findViewById(R.id.iv_actre);
			holder.iv_actj = (ImageView) convertView.findViewById(R.id.iv_actj);
			holder.iv_actd = (ImageView) convertView.findViewById(R.id.iv_actd);
			holder.tv_actcomments = (TextView) convertView.findViewById(R.id.tv_actcomments);

			holder.tv_acttoview = (TextView) convertView.findViewById(R.id.tv_acttoview);
			holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		SimpleTopicModel bean = mListData.get(position);
		if (null != bean) {
			holder.tv_title.setText(bean.getTopicName());
			if (bean.getIsHot() == 1) {// 热
				holder.iv_actre.setVisibility(View.VISIBLE);
			} else {
				holder.iv_actre.setVisibility(View.INVISIBLE);
			}
			if (bean.getIsNice() == 1) {// 精
				holder.iv_actj.setVisibility(View.VISIBLE);
			} else {
				holder.iv_actj.setVisibility(View.INVISIBLE);
			}
			if (bean.getIsTop() == 1) {// 精
				holder.iv_actd.setVisibility(View.VISIBLE);
			} else {
				holder.iv_actd.setVisibility(View.INVISIBLE);
			}
			holder.tv_actcomments.setText(String.valueOf(bean.getReadNumber()));
			holder.tv_acttoview.setText(String.valueOf(bean.getReplyNumber()));
			holder.tv_time.setText(TimeToUtil.getSystemDayFormat(bean.getCreateTime()));
		}

		return convertView;
	}

	class ViewHolder {
		private TextView tv_title;
		private ImageView iv_actre;
		private ImageView iv_actj;
		private ImageView iv_actd;

		private TextView tv_actcomments;
		private TextView tv_acttoview;
		private TextView tv_time;

	}

	@Override
	public void onPullRefresh() {
		mPageIndex = 1;
		loadData();

	}

	@Override
	public void onPullLoadMore() {
		mPageIndex += 1;
		loadData();

	}

	// private void loadData() {
	// ArrayList<String> list = new ArrayList<String>();
	// for (int i = 0; i < 10; i++) {
	// list.add("");
	// }
	// mAdapter.putData(list);
	// mListView.onLoadFinish();
	// mListView.onRefreshFinish();
	// }
	private void loadData() {
		HttpClientAsync client = HttpClientAsync.getInstance();
		HttpParams param = new HttpParams();
		param.put("barId", barId);
		param.put("filterType", "1"); // 0-默认排序 1-本吧精华 2-我发的贴 3-我回的贴
		param.put("pageIndex", mPageIndex);
		param.put("pageSize", Constants.PAGE_SIZE);

		client.get(HttpUrl.getUrl(HttpUrl.FilterTopics), null, new HttpCallBack() {

			@Override
			public void onHttpFailure(Exception arg0, String arg1) {
				if (!StringUtils.isEmpty(arg1)) {
					ToastUtils.showMessage(arg1);
				}
				mListView.onRefreshFinish();
				mListView.onLoadFinish();
			}

			@Override
			public void onHttpStarted() {

			}

			@Override
			public void onHttpSuccess(Object arg0) {
				SimpleTopicEntity entity = (SimpleTopicEntity) arg0;
				ArrayList<SimpleTopicModel> data = entity.getData();
				if (null != data) {
					mAdapter.putData(data);
				}
				mListView.onRefreshFinish();
				mListView.onLoadFinish();
			}

		}, SimpleTopicEntity.class);

	}
}
